#include "RegionGrowingSegmentationModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.regiong_rowing_segmentation_model.json";
}

RegionGrowingSegmentationModel::RegionGrowingSegmentationModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/region_growing_rgb_segmentation.html");
}

RegionGrowingSegmentationModel& RegionGrowingSegmentationModel::get()
{
    static RegionGrowingSegmentationModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void RegionGrowingSegmentationModel::fromJson(const QJsonObject& json, QStringList& errorList)
{

    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    distanceThreshold_ = json["distanceThreshold"].toDouble(distanceThreshold_);
    pointColorThreshold_ = json["pointColorThreshold"].toDouble(pointColorThreshold_);
    regionColorThreshold_ = json["regionColorThreshold"].toDouble(regionColorThreshold_);
    minClusterSize_ = json["minClusterSize"].toInt(minClusterSize_);
}

QJsonObject RegionGrowingSegmentationModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("distanceThreshold", QJsonValue(distanceThreshold_));
    json.insert("pointColorThreshold", QJsonValue(pointColorThreshold_));
    json.insert("regionColorThreshold", QJsonValue(regionColorThreshold_));
    json.insert("minClusterSize", QJsonValue(static_cast<int>(minClusterSize_)));

    return json;
}
